package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.select.IntLenDocFilter;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDImagePool;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GDCharPointPane extends AdjustablePanel {
    private JLabel lblStatPoints;
    private JLabel lblSkillPoints;
    private JLabel lblDevotionPoints;
    private JLabel lblTributePoints;
    private JLabel lblSkillReclaimPoints;
    private JLabel lblDevotionReclaimPoints;
    private JLabel lblMastery1Points;
    private JLabel lblMastery2Points;
    private JTextField ftStatPoints;
    private JTextField ftSkillPoints;
    private JTextField ftDevotionPoints;
    private JTextField ftTributePoints;
    private JTextField ftSkillReclaimPoints;
    private JTextField ftDevotionReclaimPoints;
    private JTextField ftMastery1Points;
    private JButton btnMastery1Reset;
    private JTextField ftMastery2Points;
    private JButton btnMastery2Reset;
    private GDChar.SkillInfo[] infos;
    private GDChar gdc;
    private GDCharEditTabbedPane tabPane;

    public GDCharPointPane(final GDCharEditTabbedPane tabPane) {
        this.tabPane = tabPane;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblStatPoints).addComponent(this.lblSkillPoints).addComponent(this.lblDevotionPoints).addComponent(this.lblTributePoints).addComponent(this.lblSkillReclaimPoints).addComponent(this.lblDevotionReclaimPoints).addComponent(this.lblMastery1Points).addComponent(this.lblMastery2Points)).addGroup(layout.createParallelGroup().addComponent(this.ftStatPoints).addComponent(this.ftSkillPoints).addComponent(this.ftDevotionPoints).addComponent(this.ftTributePoints).addComponent(this.ftSkillReclaimPoints).addComponent(this.ftDevotionReclaimPoints).addComponent(this.ftMastery1Points).addComponent(this.ftMastery2Points)).addGroup(layout.createParallelGroup().addComponent(this.btnMastery1Reset).addComponent(this.btnMastery2Reset));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblStatPoints).addComponent(this.ftStatPoints)).addGroup(layout.createParallelGroup().addComponent(this.lblSkillPoints).addComponent(this.ftSkillPoints)).addGroup(layout.createParallelGroup().addComponent(this.lblDevotionPoints).addComponent(this.ftDevotionPoints)).addGroup(layout.createParallelGroup().addComponent(this.lblTributePoints).addComponent(this.ftTributePoints)).addGroup(layout.createParallelGroup().addComponent(this.lblSkillReclaimPoints).addComponent(this.ftSkillReclaimPoints)).addGroup(layout.createParallelGroup().addComponent(this.lblDevotionReclaimPoints).addComponent(this.ftDevotionReclaimPoints)).addGroup(layout.createParallelGroup().addComponent(this.lblMastery1Points).addComponent(this.ftMastery1Points).addComponent(this.btnMastery1Reset)).addGroup(layout.createParallelGroup().addComponent(this.lblMastery2Points).addComponent(this.ftMastery2Points).addComponent(this.btnMastery2Reset));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblStatPoints, this.lblSkillPoints);
        layout.linkSize(0, this.lblStatPoints, this.lblDevotionPoints);
        layout.linkSize(0, this.lblStatPoints, this.lblTributePoints);
        layout.linkSize(0, this.lblStatPoints, this.lblSkillReclaimPoints);
        layout.linkSize(0, this.lblStatPoints, this.lblDevotionReclaimPoints);
        layout.linkSize(0, this.lblStatPoints, this.lblMastery1Points);
        layout.linkSize(0, this.lblStatPoints, this.lblMastery2Points);
        layout.linkSize(0, this.ftStatPoints, this.ftSkillPoints);
        layout.linkSize(0, this.ftStatPoints, this.ftDevotionPoints);
        layout.linkSize(0, this.ftStatPoints, this.ftTributePoints);
        layout.linkSize(0, this.ftStatPoints, this.ftSkillReclaimPoints);
        layout.linkSize(0, this.ftStatPoints, this.ftDevotionReclaimPoints);
        layout.linkSize(0, this.ftStatPoints, this.ftMastery1Points);
        layout.linkSize(0, this.ftStatPoints, this.ftMastery2Points);
        layout.linkSize(1, this.lblStatPoints, this.lblSkillPoints);
        layout.linkSize(1, this.lblStatPoints, this.lblDevotionPoints);
        layout.linkSize(1, this.lblStatPoints, this.lblTributePoints);
        layout.linkSize(1, this.lblStatPoints, this.lblSkillReclaimPoints);
        layout.linkSize(1, this.lblStatPoints, this.lblDevotionReclaimPoints);
        layout.linkSize(1, this.lblStatPoints, this.lblMastery1Points);
        layout.linkSize(1, this.lblStatPoints, this.lblMastery2Points);
        layout.linkSize(1, this.lblStatPoints, this.ftStatPoints);
        layout.linkSize(1, this.lblStatPoints, this.ftSkillPoints);
        layout.linkSize(1, this.lblStatPoints, this.ftDevotionPoints);
        layout.linkSize(1, this.lblStatPoints, this.ftTributePoints);
        layout.linkSize(1, this.lblStatPoints, this.ftSkillReclaimPoints);
        layout.linkSize(1, this.lblStatPoints, this.ftDevotionReclaimPoints);
        layout.linkSize(1, this.lblStatPoints, this.ftMastery1Points);
        layout.linkSize(1, this.lblStatPoints, this.ftMastery2Points);
        layout.linkSize(1, this.lblStatPoints, this.btnMastery1Reset);
        layout.linkSize(1, this.lblStatPoints, this.btnMastery2Reset);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntButton = UIManager.getDefaults().getFont("Button.font");
        if (fntButton == null) {
            fntButton = fntLabel;
        }
        Font fntFText = UIManager.getDefaults().getFont("FormattedTextField.font");
        if (fntFText == null) {
            fntFText = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntFText = fntFText.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CHAR_POINTS"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.lblStatPoints == null) {
            this.lblStatPoints = new JLabel();
        }
        this.lblStatPoints.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CHAR_POINTS_STAT"));
        this.lblStatPoints.setFont(fntLabel);
        if (this.ftStatPoints == null) {
            this.ftStatPoints = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftStatPoints.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftStatPoints.setPreferredSize(new Dimension(GDStashFrame.iniConfig.sectUI.fontSize * 4, GDStashFrame.iniConfig.sectUI.fontSize * 2));
        this.ftStatPoints.setFont(fntFText);
        if (this.lblSkillPoints == null) {
            this.lblSkillPoints = new JLabel();
        }
        this.lblSkillPoints.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CHAR_POINTS_SKILL"));
        this.lblSkillPoints.setFont(fntLabel);
        if (this.ftSkillPoints == null) {
            this.ftSkillPoints = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftSkillPoints.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftSkillPoints.setFont(fntFText);
        if (this.lblDevotionPoints == null) {
            this.lblDevotionPoints = new JLabel();
        }
        this.lblDevotionPoints.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CHAR_POINTS_DEVOTION"));
        this.lblDevotionPoints.setFont(fntLabel);
        if (this.ftDevotionPoints == null) {
            this.ftDevotionPoints = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftDevotionPoints.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftDevotionPoints.setFont(fntFText);
        if (this.lblTributePoints == null) {
            this.lblTributePoints = new JLabel();
        }
        this.lblTributePoints.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CHAR_POINTS_TRIBUTE"));
        this.lblTributePoints.setFont(fntLabel);
        if (this.ftTributePoints == null) {
            this.ftTributePoints = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftTributePoints.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftTributePoints.setFont(fntFText);
        if (this.lblSkillReclaimPoints == null) {
            this.lblSkillReclaimPoints = new JLabel();
        }
        this.lblSkillReclaimPoints.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CHAR_POINTS_SKILL_REC"));
        this.lblSkillReclaimPoints.setFont(fntLabel);
        if (this.ftSkillReclaimPoints == null) {
            this.ftSkillReclaimPoints = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftSkillReclaimPoints.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftSkillReclaimPoints.setFont(fntFText);
        if (this.lblDevotionReclaimPoints == null) {
            this.lblDevotionReclaimPoints = new JLabel();
        }
        this.lblDevotionReclaimPoints.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CHAR_POINTS_DEVOTION_REC"));
        this.lblDevotionReclaimPoints.setFont(fntLabel);
        if (this.ftDevotionReclaimPoints == null) {
            this.ftDevotionReclaimPoints = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftDevotionReclaimPoints.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftDevotionReclaimPoints.setFont(fntFText);
        if (this.lblDevotionReclaimPoints == null) {
            this.lblDevotionReclaimPoints = new JLabel();
        }
        this.lblDevotionReclaimPoints.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CHAR_POINTS_DEVOTION_REC"));
        this.lblDevotionReclaimPoints.setFont(fntLabel);
        if (this.ftDevotionReclaimPoints == null) {
            this.ftDevotionReclaimPoints = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftDevotionReclaimPoints.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftDevotionReclaimPoints.setFont(fntFText);
        if (this.lblMastery1Points == null) {
            this.lblMastery1Points = new JLabel();
        }
        if (this.infos == null) {
            this.lblMastery1Points.setText("");
        } else if (this.infos.length >= 1) {
            final Object[] args = {this.infos[0].name};
            final String s = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_CHAR_MASTERY_POINTS", args);
            this.lblMastery1Points.setText(s);
        }
        this.lblMastery1Points.setFont(fntLabel);
        if (this.ftMastery1Points == null) {
            this.ftMastery1Points = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(2);
            final AbstractDocument doc = (AbstractDocument) this.ftMastery1Points.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftMastery1Points.setFont(fntFText);
        if (this.btnMastery1Reset == null) {
            (this.btnMastery1Reset = new JButton()).addActionListener(new ResetMasteryListener(0));
        }
        this.btnMastery1Reset.setIcon(GDImagePool.iconCharEditMasteryReset16);
        this.btnMastery1Reset.setFont(fntButton);
        if (this.lblMastery2Points == null) {
            this.lblMastery2Points = new JLabel();
        }
        if (this.infos == null) {
            this.lblMastery2Points.setText("");
        } else if (this.infos.length == 1) {
            this.lblMastery2Points.setText("");
        } else {
            final Object[] args = {this.infos[1].name};
            final String s = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_CHAR_MASTERY_POINTS", args);
            this.lblMastery2Points.setText(s);
        }
        this.lblMastery2Points.setFont(fntLabel);
        if (this.ftMastery2Points == null) {
            this.ftMastery2Points = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(2);
            final AbstractDocument doc = (AbstractDocument) this.ftMastery2Points.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftMastery2Points.setFont(fntFText);
        if (this.btnMastery2Reset == null) {
            (this.btnMastery2Reset = new JButton()).addActionListener(new ResetMasteryListener(1));
        }
        this.btnMastery2Reset.setIcon(GDImagePool.iconCharEditMasteryReset16);
        this.btnMastery2Reset.setFont(fntButton);
    }

    private JPanel buildMasteryPanel(final JLabel label, final JFormattedTextField textField, final JButton btnReset) {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(label)).addGroup(layout.createParallelGroup().addComponent(textField)).addGroup(layout.createParallelGroup().addComponent(btnReset));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(label).addComponent(textField).addComponent(btnReset));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(1, label, textField);
        layout.linkSize(1, label, btnReset);
        return panel;
    }

    public void setChar(final GDChar gdc) {
        this.gdc = gdc;
        if (gdc == null) {
            this.ftStatPoints.setText("0");
            this.ftSkillPoints.setText(Integer.toString(this.tabPane.getSkillPoints()));
            this.ftDevotionPoints.setText("0");
            this.ftTributePoints.setText("0");
            this.ftSkillReclaimPoints.setText("0");
            this.ftDevotionReclaimPoints.setText("0");
            this.lblMastery1Points.setText("");
            this.lblMastery2Points.setText("");
            this.ftMastery1Points.setText("0");
            this.ftMastery2Points.setText("0");
            this.btnMastery1Reset.setEnabled(false);
            this.btnMastery2Reset.setEnabled(false);
        } else {
            this.ftStatPoints.setText(Integer.toString(gdc.getStatPoints()));
            this.ftSkillPoints.setText(Integer.toString(this.tabPane.getSkillPoints()));
            this.ftDevotionPoints.setText(Integer.toString(gdc.getDevotionPoints()));
            this.ftTributePoints.setText(Integer.toString(gdc.getTributes()));
            this.ftSkillReclaimPoints.setText(Integer.toString(gdc.getSkillReclaimPoints()));
            this.ftDevotionReclaimPoints.setText(Integer.toString(gdc.getDevotionReclaimPoints()));
            this.updateMasteryInfo();
        }
    }

    private void updateMasteryInfo() {
        this.infos = this.gdc.getMasteryInfo();
        if (this.infos == null) {
            this.lblMastery1Points.setText("");
            this.ftMastery1Points.setText("0");
            this.lblMastery2Points.setText("");
            this.ftMastery2Points.setText("0");
            this.btnMastery1Reset.setEnabled(false);
            this.btnMastery2Reset.setEnabled(false);
        } else if (this.infos.length == 1) {
            final Object[] args = {this.infos[0].name};
            final String s = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_CHAR_MASTERY_POINTS", args);
            this.lblMastery1Points.setText(s);
            this.ftMastery1Points.setText(Integer.toString(this.infos[0].points));
            this.btnMastery1Reset.setEnabled(true);
            this.lblMastery2Points.setText("");
            this.ftMastery2Points.setText("0");
            this.btnMastery2Reset.setEnabled(false);
        } else {
            final Object[] args2 = {this.infos[0].name};
            final String s2 = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_CHAR_MASTERY_POINTS", args2);
            this.lblMastery1Points.setText(s2);
            this.ftMastery1Points.setText(Integer.toString(this.infos[0].points));
            this.btnMastery1Reset.setEnabled(true);
            final Object[] args3 = {this.infos[1].name};
            final String s3 = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_CHAR_MASTERY_POINTS", args3);
            this.lblMastery2Points.setText(s3);
            this.ftMastery2Points.setText(Integer.toString(this.infos[1].points));
            this.btnMastery2Reset.setEnabled(true);
        }
    }

    public void updateChar(final GDChar gdc) {
        if (gdc == null) {
            return;
        }
        gdc.setStatPoints(Integer.parseInt(this.ftStatPoints.getText()));
        gdc.setSkillPoints(Integer.parseInt(this.ftSkillPoints.getText()));
        gdc.setDevotionPoints(Integer.parseInt(this.ftDevotionPoints.getText()));
        gdc.setTributes(Integer.parseInt(this.ftTributePoints.getText()));
        gdc.setSkillReclaimPoints(Integer.parseInt(this.ftSkillReclaimPoints.getText()));
        gdc.setDevotionReclaimPoints(Integer.parseInt(this.ftDevotionReclaimPoints.getText()));
        if (this.infos != null) {
            if (this.infos.length >= 1) {
                this.infos[0].points = Integer.parseInt(this.ftMastery1Points.getText());
            }
            if (this.infos.length >= 2) {
                this.infos[1].points = Integer.parseInt(this.ftMastery2Points.getText());
            }
        }
        gdc.setSkillLevel(this.infos);
    }

    public int getStatPoints() {
        int val = 0;
        try {
            val = Integer.parseInt(this.ftStatPoints.getText());
        } catch (NumberFormatException ex) {
            val = 0;
        }
        return val;
    }

    public void setStatPoints(final int points) {
        this.ftStatPoints.setText(Integer.toString(points));
    }

    public void setSkillPoints(final int points) {
        this.ftSkillPoints.setText(Integer.toString(points));
    }

    public void resetMastery(final String masteryID) {
        this.updateMasteryInfo();
    }

    private class ResetMasteryListener implements ActionListener {
        private int index;

        public ResetMasteryListener(final int index) {
            this.index = index;
        }

        @Override
        public void actionPerformed(final ActionEvent ev) {
            if (GDCharPointPane.this.gdc == null) {
                return;
            }
            if (GDCharPointPane.this.infos == null) {
                return;
            }
            if (GDCharPointPane.this.infos.length < this.index + 1) {
                return;
            }
            final String masteryID = GDCharPointPane.this.infos[this.index].id;
            if (masteryID == null) {
                return;
            }
            GDCharPointPane.this.tabPane.resetMastery(masteryID);
        }
    }
}
